package com.kgcx.order.service;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.order.domain.Order;
import com.kgcx.order.domain.OrderDriverPaied;
import com.kgcx.order.mapper.OrderDriverPaiedMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class OrderDriverPaiedService {

    private static Logger logger = LoggerFactory.getLogger(OrderDriverPaiedService.class);

    @Autowired
    private OrderDriverPaiedMapper odpMapper;

    /**
     * 更新司机每日已代付订单数
     * @param order
     */
    public void add(Order order) {

        try {
//            LocalDateTime localDateTime = LocalDateTime.now();
            LocalDateTime localDateTime = order.getCreateTime().toLocalDateTime();
            RequestData rd = new RequestData();
            rd.put("driver_id", order.getDriverId());
            rd.put("date_day", DateTimeUtils.convertNumberDay(localDateTime));
            OrderDriverPaied odp = odpMapper.getTodayOrderDriverPaied(rd);
            if (odp == null) {
                OrderDriverPaied odpTmp = new OrderDriverPaied();
                odpTmp.setDateMonth(DateTimeUtils.convertNumberMonth(localDateTime));
                odpTmp.setDateDay(DateTimeUtils.convertNumberDay(localDateTime));
                odpTmp.setOrderCount(1);
                odpTmp.setOrderAmount(order.getFactFinalPrice());
                odpTmp.setDriverId(order.getDriverId());
                odpTmp.setStatus(Cons.COMMON_STATUS_ACTIVE);
                odpMapper.insert(odpTmp);
            } else {
                odp.setOrderCount(odp.getOrderCount() + 1);
                odp.setOrderAmount(odp.getOrderAmount() + order.getFactFinalPrice());
                odpMapper.updateByPrimaryKey(odp);
            }
        }catch ( Exception e) {
            logger.error("增加每日已代付订单数异常:driverid:"+order.getDriverId(),e);
        }


    }
}
